import 'package:book_manager_app/app/modules/add_book/models/book_sales_req.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:book_manager_app/app/modules/home/controllers/home_controller.dart';
import 'package:getx_scaffold/getx_scaffold.dart';

import '../../../utils/widget/common.dart';
import '../../add_book/models/book.dart';

class BudgetListView extends StatelessWidget {
  const BudgetListView({super.key});

  @override
  Widget build(BuildContext context) {
    HomeController controller = Get.find();
    return Container(
      color: Colors.transparent,
      width: 360,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start, 
        children: [
          Container(
            margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
            padding: const EdgeInsets.fromLTRB(6, 4, 6, 4),
            decoration: BoxDecoration(
              color: Colors.red,
              borderRadius: BorderRadius.circular(5),
              border: Border.all(color: Colors.yellow, width: 1),
            ),
            child: const Text('待出库清单', style: TextStyle(fontSize: 16, color: Colors.yellow)),
          ),
          const SizedBox(height: 12),
          SizedBox(
            height: ScreenUtil().screenHeight,
            child: Obx(() {
              return ListView.builder(
                padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
                itemCount: controller.bookSalesReqs.length,
                itemBuilder: (context, index) {
                  BookSalesReq bookSalesReq = controller.bookSalesReqs[index];
                  String payTypeName = bookSalesReq.payType != 5 ? "出售" : "赠送";
                  return IntrinsicWidth(
                    child: Row(
                      children: [
                        Expanded(
                            child: Row(
                              children: [
                                Container(
                                  margin: const EdgeInsets.fromLTRB(0, 0, 0, 10),
                                  padding: const EdgeInsets.fromLTRB(2, 0, 0, 0),
                                  decoration: BoxDecoration(
                                    color: const Color.fromRGBO(0, 0, 0, 0.35),
                                    borderRadius: BorderRadius.circular(5),
                                  ),
                                  child: Row(
                                    children: [
                                      SizedBox(
                                        width: 32, height: 20,
                                        child: ElevatedButton(
                                          style: ElevatedButton.styleFrom(
                                            padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                                            backgroundColor: bookSalesReq.payType != 5 ? Colors.green : Colors.red,
                                            shape: RoundedRectangleBorder(
                                              borderRadius: BorderRadius.circular(4.0),
                                            ),
                                          ),
                                          child: Text(payTypeName, style: TextStyle(fontSize: 12, color: Colors.white)),
                                          onPressed: (){
                                            // 切换赠送状态
                                            String payTypeTitle = bookSalesReq.payType != 5 ? "赠送" : "出售";
                                            showAlertDialog(
                                                title: "确认$payTypeTitle",
                                                message: "您是想将《${bookSalesReq.name}》改为$payTypeTitle",
                                                cancelText: "取消", 
                                                cancel: () {
                                                  dismissAlertDialog();
                                                },
                                                confirmText: "确认$payTypeTitle", 
                                                confirm: () {
                                                  dismissAlertDialog(); // 关闭弹框
                                                  bookSalesReq.payType == 5 ? bookSalesReq.payType = 3 : bookSalesReq.payType = 5; // 取消选择
                                                  controller.bookSalesReqs.refresh(); // 刷新UI
                                                }
                                              );
                                        }),
                                      ),
                                      Text(
                                        "《${bookSalesReq.name}》",
                                        style: const TextStyle(fontSize: 14, color: Colors.white, overflow: TextOverflow.ellipsis),
                                      ),
                                      Text('¥${bookSalesReq.salePrice}', style: const TextStyle(fontSize: 10, color: Colors.yellow)),
                                      const SizedBox(width: 6),
                                      GestureDetector(
                                        child: const Icon(Icons.close, color: Colors.white, size: 22),
                                        onTap: (){
                                          showAlertDialog(
                                              title: "删除确认",
                                              message: "您是想删除《${bookSalesReq.name}》吗？",
                                              cancelText: "取消", cancel: ()=> dismissAlertDialog(),
                                              confirmText: "确认删除", confirm: () async {
                                            dismissAlertDialog(); // 关闭弹框
                                            controller.bookSalesReqs.remove(bookSalesReq);
                                            controller.bookSalesReqs.refresh();
                                          }
                                          );
                                        },
                                      ),
                                      const SizedBox(width: 6),
                                    ],
                                  ),
                                )
                              ],
                            )
                        ),
                      ],
                    ),
                  );
                },
              );
            }),
          ),
        ],
      ),
    );
  }
}
